#pragma strict
private var isTouchDevice : boolean = false;
private var canMove = false;

function Awake()
{
	if (Application.platform == RuntimePlatform.Android ||
	Application.platform == RuntimePlatform.IPhonePlayer)
	isTouchDevice = true;
	else
	isTouchDevice = false;
}

function Start ()
{
	
}

var speed : float = 0.1;

function FixedUpdate ()
{
	var clickDetected : boolean;
	var touchPosition : Vector3;
	
	// Detect click and calculate touch position
	if (isTouchDevice)
	{
		clickDetected = (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved);
		touchPosition = Input.GetTouch(0).position;
	}
	else
	{
		// clickDetected = (Input.GetMouseButton(0));
		clickDetected = (EventType.MouseDrag && Input.GetMouseButton(0));
		touchPosition = Input.mousePosition;
	}
	
	if (clickDetected)
	{
		var hit : RaycastHit;
		var ray = Camera.main.ScreenPointToRay(touchPosition);
		
		if (Physics.Raycast(ray, hit) && hit.transform.tag == "Element")
		{
			if (isTouchDevice)
			{
				// Get movement of the finger since last frame
				var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;
				
				// Move object across XY plane
				transform.Translate (-touchDeltaPosition.x * speed,
				-touchDeltaPosition.y * speed, 0);
			}
		}
		else
		{
			if(Input.GetAxis("Mouse X") < 0)
			{
				transform.Translate((Input.GetAxis("Mouse X")) * speed, 0, 0);
			}
			else if(Input.GetAxis("Mouse X") > 0)
			{
				transform.Translate((Input.GetAxis("Mouse X")) * speed, 0, 0);
			}
			
			if (Input.GetAxis("Mouse Y") > 0)
			{
				transform.Translate(0, Input.GetAxis("Mouse Y") * speed, 0);
			}
			else if (Input.GetAxis("Mouse Y") < 0)
			{
				transform.Translate(0, Input.GetAxis("Mouse Y") * speed, 0);
			}
		}
	}
}

function OnTriggerEnter (other : Collider)
{
	print("Is this shit working");
	if (other.gameObject.tag == "Cell")
	{
		Debug.Log(other.gameObject.name);
		Debug.Log("Testing");
		transform.parent = other.transform;
	}
}